#include <iostream>
using namespace std;
struct Coutry
{
    int balance;
    int id;
    Coutry()
    {
        balance = 0;
    }
    void sell(int money)
    {
        balance += money;
    }
    void buy(int money)
    {
        balance -= money;
    }
    void show()
    {
        cout<< id <<" "<<balance<<endl;
    }
};
int main()
{
    Coutry all[105];
    
    int n;
    cin >>n;
    for(int i =1 ; i<=n; i++)
        all[i].id = i;
    for(int i =1 ; i<=n; i++)
    {
        int m;
        cin>>m;
        for(int j = 0; j<m ; j++)
        {
            int a,b;
            cin>>a>>b;
            all[i].sell(b);
            all[a].buy(b);
        }
    }
    while(true)
    {
        bool ifBubble = false;
        for( int i = 1; i<=n-1; i++)
        {
            if(all[i].balance < all[i+1].balance)
            {
                ifBubble = true;
                Coutry temp = all[i];
                all[i] = all[i+1];
                all[i+1] = temp;
            }
            
        }
        if(!ifBubble)
            break;
    }
    
 for(int i =1 ; i<=n; i++)
     all[i].show();
    
}